var $,
    layer,
    table;
layui.use(['jquery', 'layer', 'table'], function () {
    $ = layui.jquery,
        layer = layui.layer,
        table = layui.table;
});

getCountComment(1, 1);
getCountComment(1, 2);
getCountComment(1, 3);
getCountComment(1, '');
//查看评论数量
function getCountComment(userType, status) {
    if (userType == 1) {
        $('.before').addClass('selectStyle');
        $('.after').removeClass('selectStyle');
    }else if (userType == 2) {
        $('.after').addClass('selectStyle');
        $('.before').removeClass('selectStyle');
    }
    if (status == 1) {
        $('#commentsNormal').children('p').addClass('selectStyle').parent().siblings().children('p').removeClass('selectStyle');

    }else if (status == 2) {
        $('#commentsBlocked').find('p').addClass('selectStyle').parent().siblings().find('p').removeClass('selectStyle');

    }else if (status == 3) {
        $('#commentsDeleted').find('p').addClass('selectStyle').parent().siblings().find('p').removeClass('selectStyle');
    }else {
        $('#allComments').find('p').addClass('selectStyle').parent().siblings().find('p').removeClass('selectStyle');
    }

    $.ajax({
        url: '/comment/getCountComment',
        data: {
          'type' : 2,
          'userType' : userType,
          'status' : status,
        },
        dataType: 'json',
        success: function (data) {
            countNum(data.data);
            if (status == 1) {
                $('#normalNum').text(data.data[0].total);
            }else if (status == 2) {
                $('#blockedNum').text(data.data[0].total);
            }else if (status == 3) {
                $('#deleteNum').text(data.data[0].total);
            }else {
                $('#allNum').text(data.data[0].total);
            }
        }
    })
};

//查看表数据
getTableData(1, "")
function getTableData(userType, status) {
    $.ajax({
        url: '/comment/getAllComments',
        data: {
          'type' : 2,
          'userType' : userType,
          'status' : status,
        },
        dataType: 'json',
        success: function (data) {
            getTable(data);
        }
    })
};

//渲染表格
function getTable(data) {
    layui.use(['table'], function () {
        table = layui.table;
        table.render({
            elem:"#demo",
            data:data.data,
            page:true,
            limit:10,
            text: {
                none: '暂无相关数据' //默认：无数据。
            },
            cols:[[
                {type:'checkbox'},
                {title:'序号', align:'center' ,width:70, templet:function (data) {
                        return data.LAY_INDEX;
                    }},
                {field:'account',title:'成员账号',align:'center'},
                {field:'userName',title:'昵称',align:'center'},
                {field:'typeName',title:'课程名称',align:'center'},
                {field:'commentDate',title:'发言时间',align:'center'},
                {field:'content',title:'发言内容',align:'center'},
                {field:'status',title:'状态',align:'center',templet:function (data) {
                        if(data.status == 1){
                            status = "正常";
                        }else if(data.status == 2){
                            status = "已拦截 ";
                        }else if(data.status == 3){
                            status = "已删除";
                        }
                        return "<a class='operation'>"+status+"</a>";
                    }},
                {title:'操作',align:'center',templet:function (data) {
                        if (data.status == 1){
                            return "<a onclick='intercept("+data.id+")' class='operation'>拦截</a>"+ '&nbsp;&nbsp;'
                                +"<a onclick='del("+data.id+")' class='operation'>删除</a>"+ '&nbsp;&nbsp;'
                                +"<a onclick='see("+data.commodityId+")' class='operation'>查看</a>";
                        }else if (data.status == 2){
                            return "<a onclick='recovery("+data.id+")' class='operation'>恢复</a>"+ '&nbsp;&nbsp;'
                                +"<a onclick='del("+data.id+")' class='operation'>删除</a>"+ '&nbsp;&nbsp;'
                                +"<a onclick='see("+data.commodityId+")' class='operation'>查看</a>";
                        }else if(data.status == 3){
                            return "<a class='operation'>已删除</a>";
                        }
                    }},
            ]]
        });
    });
};

//拦截
function intercept(commentId){
    layer.open({
        type: 1,
        title: '信息',
        content: '<div style="fount-size: 15px; margin: 30px;">您确定要拦截此条信息吗</div>',
        area: ['300px', '200px'],
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            updateComment(commentId, 2);
        },
        btn2: function (index, layero) {
            layer.close(index);
        }
    });
};

//恢复
function recovery (commentId){
    layer.open({
        type: 1,
        title: '信息',
        content: '<div style="fount-size: 15px; margin:  30px;">您确定要恢复此条信息吗</div>',
        area: ['300px', '200px'],
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            updateComment(commentId, 1);
        },
        btn2: function (index, layero){
            layer.close(index);
        }
    });
};

//删除
function del (commentId){
    layer.open({
        type: 1,
        title: '信息',
        content: '<div style="color: red; fount-size: 15px; margin: 30px;">您确定要删除此条信息吗</div>',
        area: ['300px', '200px'],
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            updateComment(commentId, 3);
        },
        btn2: function (index, layero) {
            layer.close(index);
        }
    });
};

//查看
function see  (commentId){
    layer.open({
        type:2,
        title:'课程详情',
        Boolean:true,
        maxmin: true,
        area: ['80%', '80%'],
        content:"/comment/course/courseView.html?tx=" + encodeURI(commentId)
    });
};

//根据id修改评论信息
function updateComment(commentId, status){
    $.ajax({
        url: '/comment/updateComment',
        data: {
            'id' : commentId,
            'status' : status,
        },
        dataType: 'json',
        success: function (data) {
            console.log(data)
            if (data.data){
                layer.msg('修改成功');
                window.location.reload()  //刷新页面
            }else {
                layer.msg('修改失败');
            }
        }
    })
};

//选择
$(document).ready(function(){
    //判断是前端还是后端
    var userType = 1;
    var status = '';
    $('.before').on('click', function(){
        userType = 1;
        getCountComment(userType, status);
        getCountComment(1, 1);
        getCountComment(1, 2);
        getCountComment(1, 3);
        getCountComment(1, '');
        getTableData(userType, status);
    });
    $('.after').on('click', function(){
        userType = 2;
        getCountComment(userType, status);
        getCountComment(2, 1);
        getCountComment(2, 2);
        getCountComment(2, 3);
        getCountComment(2, '');
        getTableData(userType, status);
    });

    $('#allComments').on('click', function(){
        status = '';
        getCountComment(userType, status);
        getTableData(userType, status);
    });
    $('#commentsNormal').on('click', function(){
        status = 1;
        getCountComment(userType, status);
        getTableData(userType, status);
    });
    $('#commentsBlocked').on('click', function(){
        status = 2;
        getCountComment(userType, status);
        getTableData(userType, status);
    });
    $('#commentsDeleted').on('click', function(){
        status = 3;
        getCountComment(userType, status);
        getTableData(userType, status);
    });




});

//评论次数
function countNum(data){
    layui.use(['table'], function () {
        table = layui.table;
        table.render({
            elem:"#numberOfPeople",
            width:1330,
            data:data,
            cols:[[
                {field:"total",title:'历史评论次数',align:'center',width:331},
                {field:"totalPeople",title:'历史评论人数',align:'center',width:331},
                {field:"todayTotal",title:'今日评论次数',align:'center',width:331},
                {field:"todayTotalPeople",title:'今日评论人数',align:'center',width:331}
            ]]
        });
    });
};

//返回首页
function backOff(){
    parent.jumpSystemHome();
}